<?php
class mainComponents extends sfComponents
{
    public function executeMenu()
    {
        $this->selected_menu = '';
        if($this->getRequestParameter('cat1'))
        {
            $this->selected_menu = $this->getRequestParameter('cat1');
        }
        $criteria = new Criteria();
        $criteria->add(CategoriasPeer::PADRE, 0);
        $criteria->add(CategoriasPeer::ACTIVO, 1);
        $this->menues = CategoriasPeer::doSelect($criteria);
        
    }
    
    public function executeSidebar()
    {
        $this->categories = array();
        
        if($this->getRequestParameter('cat1'))
        {
            $selected_cat1 = $this->getRequestParameter('cat1');
            $num_cat1 = $this->getRequestParameter('ncat1');
            $url = '/category/'.$selected_cat1.'/'.$num_cat1;
            $criteria = new Criteria();
            $criteria->add(CategoriasPeer::PADRE, $num_cat1);
            $criteria->add(CategoriasPeer::ACTIVO, 1);
            $sublevel1 = CategoriasPeer::doSelect($criteria);
        }
        
        if($this->getRequestParameter('cat2'))
        {
            $selected_cat2 = $this->getRequestParameter('cat2');
            $num_cat2 = $this->getRequestParameter('ncat2');
            $criteria = new Criteria();
            $criteria->add(CategoriasPeer::PADRE, $num_cat2);
            $criteria->add(CategoriasPeer::ACTIVO, 1);
            $sublevel2 = CategoriasPeer::doSelect($criteria);
        }
        
        if($this->getRequestParameter('cat3'))
        {
            $selected_cat3 = $this->getRequestParameter('cat3');
            $num_cat3 = $this->getRequestParameter('ncat3');
            $criteria = new Criteria();
            $criteria->add(CategoriasPeer::PADRE, $num_cat3);
            $criteria->add(CategoriasPeer::ACTIVO, 1);
            $sublevel3 = CategoriasPeer::doSelect($criteria);
        }
        
        if(isset($sublevel1)){
            foreach($sublevel1 as $slevel1){
                $url1 = $url.'/'.$slevel1->getNombre().'/'.$slevel1->getId();
                $this->categories[] = array( 'level'=>'1', 'name' => $slevel1->getNombre(), 'url' => $url1);

                if(isset($num_cat2) && $num_cat2 == $slevel1->getId()){
                    foreach($sublevel2 as $slevel2){
                        $url2 = $url1.'/'.$slevel2->getNombre().'/'.$slevel2->getId();
                        $this->categories[] = array( 'level'=>'2', 'name' => $slevel2->getNombre(), 'url' => $url2);

                        if(isset($num_cat3) && $num_cat3 == $slevel2->getId()){
                            foreach($sublevel3 as $slevel3){
                                $url3 = $url2.'/'.$slevel3->getNombre().'/'.$slevel3->getId();
                                $this->categories[] = array( 'level'=>'2', 'name' => $slevel2->getNombre(), 'url' => $url2);
                            }
                        }
                    }
                }
            }
        }
    }
}
